package com.ifras.project.services;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

public class MyService extends Service {

	private static final String TAG = "MyService";
	int TIMEOUT_MILLISEC = 6000000;
	
	@Override
	public IBinder onBind(Intent arg0) {
		return null;
	}

	@Override
	public void onCreate() {

		Toast.makeText(this, "Congrats! MyService Created", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onCreate");
	}

	@Override
	public void onStart(Intent intent, int startId) {
		
		Toast.makeText(this, "Congrats! MyService onStart", Toast.LENGTH_LONG).show();
	}


	
	@Override
	public void onDestroy() {
		Toast.makeText(this, "MyService Stopped", Toast.LENGTH_LONG).show();
		Log.d(TAG, "onDestroy");
	}
	
	
	
	/**
	 * 
	 * @author ifras
	 * 
	 */
	/*class SyncData extends AsyncTask<String, String, String> {

		protected String doInBackground(String... args) {
			try {
				
				//DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
				//Calendar cal = Calendar.getInstance();
				

				JSONObject json = new JSONObject();
				
				json.put("pin", pinValue);
				json.put("pincode", Location);
				//json.put("date_time",dateFormat.format(cal.getTime()));

				// json.put("contactsList", contactsList);

				HttpParams httpParams = new BasicHttpParams();
				HttpConnectionParams.setConnectionTimeout(httpParams,TIMEOUT_MILLISEC);
				HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
				HttpClient httpclient = new DefaultHttpClient(httpParams);

				String url = Login_main.IP + "cellid_json.php";

				HttpPost request = new HttpPost(url);

				request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
				request.setHeader("json", json.toString());

				ResponseHandler<String> responseHandler = new BasicResponseHandler();
				String responseBody = httpclient.execute(request,
						responseHandler);

				// Parse
				JSONObject json1 = new JSONObject(responseBody);
				JSONArray jArray = json1.getJSONArray("posts");

				if (jArray.length() != 0) {

					for (int i = 0; i < jArray.length(); i++) {

						JSONObject e = jArray.getJSONObject(i);
						String s = e.getString("post");
						JSONObject jObject = new JSONObject(s);

						if (jObject.getString("message").equals("1")) {
							
							Log.v("success","successfully");
						}

						else {
							 Log.v("success","Failed");
						}

					}

				}

				else {
					// Toast.makeText(getBaseContext(),"Error",
					// Toast.LENGTH_LONG).show();
				}

			} catch (Exception e) {
				// TODO: handle exception
			}

			return null;
		}

	}
	*/
	
	
}
